package DAO;

import java.awt.Font;
import java.awt.event.*;
import java.sql.*;

import javax.swing.*;

import Main.DBMain;

public class ModifyCourse extends JFrame {
	private JPanel contentPane = new JPanel();
	private JLabel lab_Tip, lab_stuNo, lab_Course;
	private JTextField stuNoField, scoreField;
	private JButton btn_Modify, btn_Exit;
	private String course;

	public ModifyCourse(String user) {
		setTitle("录入成绩");
		setSize(500, 300);
		setLocationRelativeTo(null);
		setContentPane(contentPane);
		contentPane.setLayout(null);

		getCourse(user);// 获取科目名称

		lab_Tip = new JLabel("温馨提示：您只能修改您所教的科目！");
		lab_Tip.setBounds(100, 10, 300, 20);
		lab_Tip.setFont(new Font("宋体", Font.PLAIN, 16));
		contentPane.add(lab_Tip);

		lab_stuNo = new JLabel("请输入学生学号：");
		lab_stuNo.setBounds(105, 60, 130, 20);
		lab_stuNo.setFont(new Font("宋体", Font.PLAIN, 16));
		contentPane.add(lab_stuNo);

		stuNoField = new JTextField();
		stuNoField.setBounds(230, 60, 130, 20);
		contentPane.add(stuNoField);

		lab_Course = new JLabel("请输入新的成绩：");
		lab_Course.setBounds(105, 100, 130, 20);
		lab_Course.setFont(new Font("宋体", Font.PLAIN, 16));
		contentPane.add(lab_Course);

		scoreField = new JTextField();
		scoreField.setBounds(230, 100, 130, 20);
		/* 添加回车监听，修改 */
		scoreField.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER)
					modify(course);
			}
		});
		contentPane.add(scoreField);

		btn_Modify = new JButton("点击修改");
		btn_Modify.setBounds(105, 160, 120, 40);
		btn_Modify.setFont(new Font("宋体", Font.BOLD, 16));
		/* 添加按钮监听 */
		btn_Modify.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				modify(course);
			}
		});
		contentPane.add(btn_Modify);

		btn_Exit = new JButton("取消");
		btn_Exit.setBounds(240, 160, 120, 40);
		btn_Exit.setFont(new Font("宋体", Font.BOLD, 16));
		btn_Exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		contentPane.add(btn_Exit);
		setVisible(true);
	}

	public void getCourse(String teaName) {
		Connection conn;
		Statement stmt;
		ResultSet rs;
		try {
			conn = DBMain.getConnection();
			stmt = conn.createStatement();
			String sql = "select * from tea_info where teaName= '" + teaName + "'";// 查询语句
			rs = stmt.executeQuery(sql);
			// 查询到该老师并获取到教的科目
			if (rs.next()) {
				course = rs.getString(2);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void modify(String stuCourse) {
		PreparedStatement pstmt = null;
		Connection conn = null;
		String sql = "";
		String getStuNo = stuNoField.getText().trim();
		/* 输入成绩不为空，进行修改 */
		if (!("".equals(getStuNo))) {
			if (!("".equals(scoreField.getText()))) {
				try {
					conn = DBMain.getConnection();
					Double getScore = Double.valueOf(scoreField.getText().trim());
					sql = "update stu_info set `" + stuCourse + "` =? where stuNo='" + getStuNo + "'";
					pstmt = conn.prepareStatement(sql);
					pstmt.setDouble(1, getScore);
					pstmt.executeUpdate();
					JOptionPane.showMessageDialog(null, "修改成功！");
					setVisible(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				JOptionPane.showMessageDialog(null, "成绩为空！请重新输入");
			}
		} else {
			JOptionPane.showMessageDialog(null, "学号为空！请重新输入");
		}
	}
}
